﻿namespace UDKDevKitVSXDebugEngine.Classes
{
    using System;
    using System.Diagnostics;
    using UDKDevKitVSXCore.Classes;

    public class UDKConduitEventsListener : IpcCommandListener
    {
        private ExecSimulator execSimulator;

        public UDKConduitEventsListener(ExecSimulator execSimulator) : base("VSSide")
        {
            if (execSimulator == null)
            {
                Debug.Fail("ArgumentNullException - execSimulator");
                throw new ArgumentNullException("execSimulator");
            }

            this.execSimulator = execSimulator;
        }

        protected override void OnClose()
        {
        }

        protected override void OnProcessCommand(string command)
        {
            if (string.IsNullOrEmpty(command))
            {
                Debug.Fail("ArgumentException - command");
                throw new ArgumentException("command");
            }

            if ("Stop".Equals(command))
            {
                // send stop signal to debugger
                Debug.WriteLine("UDKConduitEventsListener: STOP");

                this.execSimulator.RaiseDebugEventsBreakpoint();
                return;
            }

            if (command.StartsWith("EditorLoadClass"))
            {
                this.execSimulator.LoadDocumentFile(command.Substring("EditorLoadClass".Length + 1));
                return;
            }

            if (command.StartsWith("EditorGotoLine"))
            {
                this.execSimulator.SetStatementLine(command.Substring("EditorGotoLine".Length + 1));
                return;
            }

            if (command.StartsWith("AddWatches"))
            {
                this.execSimulator.AddWatches(command.Substring("AddWatches".Length + 1));
                return;
            }

            if (command.StartsWith("CallStackClear"))
            {
                this.execSimulator.CallStackClear();
                return;
            }

            if (command.StartsWith("CallStackAdd"))
            {
                this.execSimulator.CallStackAdd(command.Substring("CallStackAdd".Length + 1));
                return;
            }

            if (command.StartsWith("System::TerminateDebuggingProcess"))
            {
                this.execSimulator.RaiseDebugEventsOnTerminateProcess();
                StopListening();
                OnClose();
                return;
            }
        }
    }
}
